<?php
namespace control;

/*
*待办事项类
*/
class backlogClass extends adIndexClass
{
    /**
     * 查询表单
     *
     * @param string $workFlow
     * @return string
     */
    public function homeForm(string $workFlow)
    {
        if ($workFlow == '待处理') {
            $title = L('person.table.reminderDate').' - '.L('person.table.reminderDate');
            $type = 'date';
            $style = ' width200';

        } else {
            $title = L('person.table.processingTime').' - '.L('person.table.processingTime');
            $type = 'datetime';
            $style = ' width300';
            $workFlow = '已完成';
        }
        $html =
            text(["name" => "text","title" => L('person.table.nei_rong')]).
            hidden(["name" => "workFlow", 'value'=> $workFlow]).
            text(["name" => "date", "title" => $title, 'class'=>'text'.$style, 'laydate' => [ 'range' => '~', 'type'=> $type] ]);
        return $this->search($html, 'menu');
    }

    /**
     * 行政人事-待办事项-查询条件
     *
     * @param string $workFlow
     * @return string
     */
    public function home($workFlow = '待处理')
    {
        if ($workFlow == '待处理') {
            $data = [
                "id" => "all",
                "menu" => [
                    "pending" => L('person.table.pending'),
                    "processed" => L('person.table.processed'),
                ]
            ];
        }
        $data['html'] = $this->homeForm('待处理');
        return $this->tab($data);
    }

    /**
     * 已处理
     *
     */
    public function processed()
    {
        $json['html'] = $this->homeForm('已处理');
        return json_encode($json, 320);
    }
    public function processedSearch()
    {
        return $this->homeSearch();
    }


    /**
     * 查询结果
     *
     * @return string
     */
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "text" => $this->post('text'),//待办事项说明
        );
        //全等查询数组
        $equal = array(
            "workFlow" => $this->post('workFlow'),//办理状态（待处理/已完成）
        );
        //设置查询变量为空
        $and = " and stid = '".self::$stid."' ";
        if (!empty($this->post('date'))) {
            if ($this->post('workFlow') == '待处理') {
                $and .= self::timeComparison($this->post('date'), 'date', 'date');

            } else {
                $and .= self::timeComparison($this->post('time'), 'time', 'updateTime');
            }
        }
        //排序
        $orderBy = $this->orderBy(array(
            "search"  => $this->post('orderBy') ?? null,
            "default" => "创建时间降序",
            "array"   => array(
                "startDay"   => array("desc" => "提示日期降序","site" => 5),
                "updateTime" => array("desc" => "处理时间降序","site" => 6),
                "time"       => array("desc" => "创建时间降序","site" => 7),
            ),
        ));
        if ($this->post('workFlow') == '待处理') {
            $orderBy['orderBy'] = "FIELD(urging,'是','否'),".$orderBy['orderBy'];
        }
        //获取查询结果
        $where = search(array("like" => $like,"=" => $equal)).$and;
        $page = $this->page(["table" => "backlog","where" => $where, "order" => $orderBy['orderBy']]);
        //获取表头
        if ($this->post('workFlow') == '待处理') {
            $checkAll = $this->checkAll("listForm", "listId");
            $tr[] = array($checkAll,L('person.table.nei_rong'), L('person.table.ti_shi_ri_qi'));
        } else {
            $tr[] = array(L('person.table.nei_rong'), L('person.table.ti_shi_ri_qi'), L('person.table.chu_li_shi_jian'));
        }
        //获取记录
        foreach ($page['array'] as $array) {
            $info = query('audit', " id ='$array[typeId]'");
            if (!empty($array['targetCla']) && !empty($array['targetFun']) && empty($info['id'])) {
                $click= "newIframe('{$array['type']}详情','".root."control/adIndex.html?cla={$array['targetCla']}&fun={$array['targetFun']}&id={$array['typeId']}')";
            }else{
                $click= "newIframe('{$array['type']}详情','".root."control/adIndex.html?cla={$info['targetCla']}&fun={$info['targetFun']}&id={$info['targetId']}')";
            }
            if ($array['workFlow'] == '待处理') {
                if ($array['urging'] =='是'){
                    $array['text']="<span style='color: red'>".L('person.table.urge')."</span>".$array['text'];
                }
                //列表控件
                $dominate = array(
                    "box" => array("name" => "listId", "value" => $array['id']),
                );
                $tr[] = [$this->dominate($dominate),"<span class='underline' style='cursor:pointer' onclick=\"{$click}\">".$array['text']."</span>", $array['startDay']];
            } else {
                $tr[] = ["<span class='underline' style='cursor:pointer' onclick=\"{$click}\">".$array['text']."</span>", $array['startDay'], $array['updateTime']];
            }
        }
        $button = '';
//        if ($this->post('workFlow') == '待处理') {
//            $button = "<span onclick=\"layer('backlog','layerModel',{type:'read'})\" class='spanButton'>标记为已读</span>\n";
//        }
        $json['html'] = $page['hint'].$button."<form name='listForm'>".space.$this->tableMany(array("tr" => $tr,"orderBySign" => $orderBy))."</form>".$page['button'].clear;
        return json_encode($json, 320);
    }


    /**
     * 弹窗
     * @return bool|string
     */
    public function layerModel(): bool|string
    {
        //准备数据
        $html = $this->layerEdit(array("table" => array(
            array(
                must . L('person.table.tip')."：" => L('person.table.sfbjzxdbxxwywc'),
            )
        ), 'formTwo' => 'listForm'));

        return $this->layer(array("title" => L('person.table.biao_ji'), "height" => 180, "html" => $html));
    }

    /**
     *弹窗编辑
     */
    public function layerModelEdit(): bool|string
    {
        $listId = $this->post('listId');
        if (empty($listId) || !is_array($listId)) {
            $json['warn'] = L('person.table.qxzxybjwywcddbxx');
        } else {
            $json['warn'] = update(array("table" => "backlog", "field" => array('workFlow' => '已完成'), "where" => "id IN ('" . implode("','", $listId) . "') and workFlow = '待处理'"));
        }
        if (in_array($json['warn'], array("新增成功", "更新成功"))) {
            //添加日志
            $text = str_replace(['{{staffName}}'],[$this->control['name']],L('person.table.markDone'));
            $this->logText(array("type" => "backlog", "text" => $text));
            //返回
            $_SESSION['warn'] = L('person.table.bjcg');
            $json['warn'] = 2;
        }
        return json_encode($json, 320);
    }
}
